<template>
  <div class="app-container">
    <el-form ref="form" :model="form">
      <el-divider content-position="left"><h3>腾讯云配置</h3></el-divider>
      <el-form-item label="腾讯云SecretId" :label-width="width" style="width: 600px">
        <el-input v-model="form.tencentcloud.SecretId" placeholder="腾讯云SecretId" autocomplete="off" clearable />
      </el-form-item>
      <el-form-item label="腾讯云SecretKey" :label-width="width">
        <el-input v-model="form.tencentcloud.SecretKey" placeholder="腾讯云SecretKey" autocomplete="off" clearable />
      </el-form-item>
      <el-form-item style="margin: auto">
        <el-button type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'tencentcloud',
  data() {
    return {
      width: '180px',
      form: {
        // 腾讯云配置
        tencentcloud: {
          SecretId: '',
          SecretKey: '',
        },
        parent: 'tencentcloud'
      },
      rules: {
      }
    }
  },
  computed: {
    ConfigModel() {
      return this.$store.$db().model('config');
    },
  },
  created() {
    this.init();
 /*   this.$http.get('/config/tencentcloud').then(response => {
      const wechat = response.data
      Object.keys(wechat).forEach(k => {
        Object.keys(wechat[k]).forEach(key => {
          if (key === 'is_cname') {
            this.form[k][key] = parseInt(wechat[k][key])
          } else if (key === 'read_from_cdn') {
            this.form[k][key] = parseInt(wechat[k][key])
          } else {
            this.form[k][key] = wechat[k][key]
          }
        })
      })
    }) */
  },
  methods: {
    async init() {
      let configData = this.ConfigModel.all();
      if(!configData){  // 如果前端缓存没有，从接口获取数据
        console.log("initData");
        await this.ConfigModel.fetchBy();
      }
      let tabC = this.ConfigModel.query().where('component','tencentcloud').first();
      const tabData = this.ConfigModel.query().where('pid',tabC.id).get();
      Object.keys(tabData).forEach(k => {
        Object.keys(tabData[k]).forEach(key => {
          if(key == 'key'){
            let keyField = tabData[k][key];
            let keyArr = keyField.split('.');
            this.form[keyArr[0]][keyArr[1]] = tabData[k]['value'];
          }
        })
      });
    },
    async submit() {
      this.verify();
    },
    async verify(){
      let valid = await this.$refs['form'].validate().catch((err)=>{return err});
      if(valid){
        //验证通过的代码的
        let result = await this.ConfigModel.sync(this.form);               //同步式模型提交
        this.$message.success(result.response.data.message);
        /*     this.$http.post('config', this.form).then(response => {    //回调式嵌套提交
              this.$message.success(response.message)
            }) */
      }else{
        //验证不通过的代码
      }
    }
  }
}
</script>

<style scoped>
  .el-form .el-form-item {
    width: 600px;
  }
</style>
